package hs.bank.Data;

import hs.bank.Entity.Card;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Repository
@Mapper
public interface CardMapper {

    @Select("select userId from card where cardNumber = #{cardNumber}")
    int getUserIdByCardNumber(@Param("cardNumber") int cardNumber);

    @Select("select cardId, cardNumber, userId, balance from card where cardNumber = #{cardNumber}")
    Card getByCardNumber(@Param("cardNumber") int cardNumber);

    @Select("select count(cardId) from card")
    int getCount();

    @Select("select max(cardNumber) from card")
    int maxCardNumber();

    @Insert("insert into card(cardNumber, userId) value(#{cardNumber}, #{userId})")
    void save(Card card);

    @Select("select cardId, cardNumber, userId, balance from card where userId = #{userId}")
    List<Card> getByUserId(@Param("userId") int userId);

    @Update("update card set balance = balance + #{amount} where cardNumber = #{cardNumber}")
    void deposit(@Param("amount") double amount, @Param("cardNumber") int cardNumber);
}
